package org.example.erp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.erp.domain.ProClientOrder;
import org.example.erp.domain.ProWorkorder;
import org.example.erp.domain.ProWorkorderBom;
import org.example.erp.domain.ProductBom;
import org.example.erp.service.impl.ProWorkorderBomServiceImpl;
import org.example.erp.service.impl.ProWorkorderServiceImpl;
import org.example.erp.service.impl.ProductBomServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@RequestMapping("/api")
@RestController
public class ProWorkOrderController {

    @Autowired
    ProWorkorderServiceImpl proWorkOrderService;//工单主表
    @Autowired
    ProWorkorderBomServiceImpl proWorkorderBomService;//工单从表BOM组成表

    @GetMapping("/processTickets/find")//查询工单
    public DataResult find(
            @RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String workorderCode,
            @RequestParam(required = false) String workorderName,
            @RequestParam(required = false) String sourceCode,
            @RequestParam(required = false) String productCode,
            @RequestParam(required = false) String productName,
            @RequestParam(required = false) String clientCode,
            @RequestParam(required = false) String clientName,
            @RequestParam(required = false) String workorderType,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date requestDate
    ) {
        Page<ProWorkorder> page = new Page<>(currentPage, pageSize);
        Page<ProWorkorder> pageList = proWorkOrderService.findPageList(workorderCode, workorderName, sourceCode, productCode, productName, clientCode, clientName, workorderType, requestDate, page);
        return DataResult.success(pageList);
    }

    @PostMapping("/processTickets/insert")//新增工单
    public DataResult insert(@RequestBody ProWorkorder proWorkorder) {
        boolean save = proWorkOrderService.add(proWorkorder);
        if (save) {
            Integer workorderId = proWorkorder.getWorkorderId();
            return DataResult.success(workorderId);
        } else {
            return DataResult.error("工单新增失败");
        }
    }


    @PutMapping("/processTickets/update")//修改工单(主从)
    public DataResult update(@RequestBody ProWorkorder proWorkorder) {
        boolean b = proWorkOrderService.updateA(proWorkorder);
        if (b) {
            return DataResult.success(b ? "成功" : "失败");
        } else {
            return DataResult.error("工单修改失败");
        }
    }

    @PutMapping("/processTickets/updateStatus")//修改工单(状态)
    public DataResult updateStatus(@RequestBody ProWorkorder proWorkorder) {
        boolean b = proWorkOrderService.updateById(proWorkorder);
        if (b) {
            return DataResult.success(b ? "成功" : "失败");
        } else {
            return DataResult.error("工单修改失败");
        }
    }

    @DeleteMapping("/processTickets/delete")//删除工单
    public DataResult remove(@RequestBody List<Long> ids){
        boolean b = proWorkOrderService.removeByIds(ids);
        if (b){
            return DataResult.success(b ? "成功" : "删除");
        }else {
            return DataResult.error("客户订单删除失败");
        }
    }

    @GetMapping("/processTickets/findBom")//查询工单Bom组成
    public DataResult findBom(
            @RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            String productId,
            String workorderId
    ) {
        Page<ProWorkorderBom> page = new Page<>(currentPage, pageSize);
        QueryWrapper<ProWorkorderBom> queryWrapper = new QueryWrapper<>();
        if (productId != null && !"".equals(productId)) {
            queryWrapper.lambda().like(ProWorkorderBom::getItemId, productId);
        }
        if (workorderId != null && !"".equals(workorderId)) {
            queryWrapper.lambda().like(ProWorkorderBom::getWorkorderId, workorderId);
        }

        page = proWorkorderBomService.page(page, queryWrapper);
        return DataResult.success(page);
    }

    @PutMapping("/processTickets/updateBom")
    public DataResult updateBom(@RequestBody ProWorkorderBom proWorkorderBom) {
        boolean b = proWorkorderBomService.updateById(proWorkorderBom);
        if (b) {
            return DataResult.success(b ? "成功" : "失败");
        } else {
            return DataResult.error("修改工单Bom失败");
        }
    }

    @GetMapping("/processTickets/findStatus")
    public DataResult find(
            @RequestParam(required = false) String workorderCode,
            @RequestParam(required = false) String workorderName,
            @RequestParam(required = false) String productCode,
            @RequestParam(required = false) String productName,
            @RequestParam(required = false) String status
    ) {
        QueryWrapper<ProWorkorder> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ProWorkorder::getStatus, "CONFIRMED");
        if (workorderCode != null && !"".equals(workorderCode)) {
            queryWrapper.lambda().like(ProWorkorder::getWorkorderCode, workorderCode);
        }
        if (workorderName != null && !"".equals(workorderName)) {
            queryWrapper.lambda().like(ProWorkorder::getWorkorderName, workorderName);
        }
        if (productCode != null && !"".equals(productCode)) {
            queryWrapper.lambda().like(ProWorkorder::getProductCode, productCode);
        }
        if (productName != null && !"".equals(productName)) {
            queryWrapper.lambda().like(ProWorkorder::getProductName, productName);
        }
        List<ProWorkorder> list = proWorkOrderService.list(queryWrapper);
        return DataResult.success(list);
    }

    @GetMapping("/processTickets/findtow")
    public DataResult findtow(@RequestParam(required = false) String workorderCode) {
        return DataResult.success(proWorkOrderService.find(workorderCode));
    }


}
